package com.aierkeji.call.cms.util;

import java.util.List;
import java.util.Map;

public class CsvUtils {
	/** CSV文件列分隔符 */
	private static final String CSV_COLUMN_SEPARATOR = ",";

	/** CSV文件列分隔符 */
	private static final String CSV_RN = "\r\n";
	private static final String CSV_DATE_STRING = "\t";
	
	
	

	public static String getCsvColumnSeparator() {
		return CSV_COLUMN_SEPARATOR;
	}

	public static String getCsvDateString() {
		return CSV_DATE_STRING;
	}

	/**
	 *
	 * @param dataList 集合数据
	 * @param colNames 表头部数据
	 * @param mapKey   查找的对应数据
	 * @param os       返回结果
	 */
	public static byte[] doExport(List<Map<String, Object>> dataList, String colNames, String mapKey) {
		try {
			StringBuffer buf = new StringBuffer();

			String[] colNamesArr = null;
			String[] mapKeyArr = null;

			colNamesArr = colNames.split(",");
			mapKeyArr = mapKey.split(",");

			// 完成数据csv文件的封装
			// 输出列头
			for (String aColNamesArr : colNamesArr) {
				buf.append(aColNamesArr).append(CSV_COLUMN_SEPARATOR);
			}
			buf.append(CSV_RN);

			if (null != dataList) { // 输出数据
				for (Map<String, Object> aDataList : dataList) {
					for (String aMapKeyArr : mapKeyArr) {
						buf.append(aDataList.get(aMapKeyArr)).append(CSV_COLUMN_SEPARATOR);
					}
					buf.append(CSV_RN);
				}
			}
			return buf.toString().getBytes("GBK");
		} catch (Exception e) {
		}
		return null;
	}

	public static String doExport(Boolean last, String... args) {
		StringBuffer buf = new StringBuffer();

		// 完成数据csv文件的封装
		// 输出列头
		for (String aColNamesArr : args) {
			buf.append(StringUtils.changeNull(aColNamesArr)).append(CSV_COLUMN_SEPARATOR);
		}
		if (!last) {
			buf.append(CSV_RN);
		}

		return buf.toString();
	}
	
//	public static String doExport(Boolean last, String[] args) {
//		StringBuffer buf = new StringBuffer();
//
//		// 完成数据csv文件的封装
//		// 输出列头
//		for (String aColNamesArr : args) {
//			buf.append(aColNamesArr).append(CSV_COLUMN_SEPARATOR);
//		}
//		if (!last) {
//			buf.append(CSV_RN);
//		}
//
//		return buf.toString();
//	}
}
